import {mapActions,mapGetters, mapMutations} from 'vuex'
import {mode,method} from "@/config/mode-config/config";
import {contain, push} from "@/common/js/utils";


//最小化适配
export const playListMixin = {
    computed: {
        ...mapGetters('song', ['playlist'])
    },
    methods: {
        handlePosition(){
            throw new Error('must implements handlePosition method')
        }
    },
    activated(){
        this.handlePosition()
    },
    watch: {
        playlist(list){
            if(!list.length)  return
            this.handlePosition()
        }
    }
}

// 模式切换
export const modeChange = {
    methods: {
        ...mapMutations('song',['setMode']),
        changeMode(){
            this.setMode((this.mode + 1) % 3)
        }
    },
    computed: {
        ...mapGetters('song',['mode']),
        modeCls(){
            if(this.mode == mode.sequence)
                return 'icon-sequence'
            else if(this.mode == mode.random)
                return 'icon-random'
            else
                return 'icon-loop'
        },
        modeTxt(){
            if(this.mode == mode.sequence)
                return '顺序播放'
            else if(this.mode == mode.random)
                return '随机播放'
            else
                return '循环播放'
        }
    }
}


//喜爱歌曲操作
export const likeListOptions = {
    computed: {
        ...mapGetters('user', ['likes'])
    },
     methods: {
         ...mapMutations('user',['addLike','removeLike','addRecent']),
         add(song){
             this.addLike(song)
         },
         remove(song){
             this.removeLike(song)
         },
         likeIcon(song){
             return contain(this.likes,song) ? 'icon-favorite red':'icon-not-favorite'
         },
         toggleLike(song){
             contain(this.likes,song) ? this.removeLike(song) : this.addLike(song)
         }
     }
}

//搜索操作
export const searchOptions = {
    computed: {
        ...mapGetters('search',['query']),
        confirm(){
            return this.$store.getters.confirm
        }
    },
    methods: {
        ...mapMutations('search',['setQuery','removeHistory','addHistory','clearHistory']),
        inputChange(val){
            this.setQuery(val)
            if(val)
                this.addHistory(val)
        },
        toConfirm(flag){
            if(!flag)    return
            this.clearHistory()
        },
        removeAll(){
            this.$store.commit('setConfirm',true)
        },
        goSearch(hot){
            this.setQuery(hot)
            this.addHistory(hot)
        },
        changeHistory(name){
            this.addHistory(name)
            this.setQuery('')
        },
        selectRecord(record){
            this.setQuery(record)
        }
    }
}

//选择添加歌曲
export const selectOptions = {
    computed: {
        ...mapGetters(['selectFlag'])
    },
    methods: {
        ...mapMutations(['setFlag']),
        ...mapActions('song',['select','single']),
        selectSong({list,index,song}){
            if(this.selectFlag == method.addSingle){
                this.single({
                    song,
                    index: 0
                })
            }else {
                this.select({
                    list: push(list,index),
                    index: 0
                })
            }

        }
    }
}

//随机播放全部
export const randomPlayOptions = {
    methods: {
        ...mapActions('song',['random']),
        randomPlay(list){
            if(!list.length)  return
            this.random(list)
        }
    }
}



